#!/bin/sh

. "$SPEC_PATH/abstract/dbops-restart"

e2e_test_extra_hash() {
  "$SHELL" "$PROJECT_PATH/stackgres-k8s/ci/build/build-functions.sh" path_hash \
    "$(realpath --relative-to "$PROJECT_PATH" "$SPEC_PATH/abstract/dbops-restart")"
}

e2e_test_install() {
  DBOPS_FAILING_NAME="$(get_sgdbops_name failing-restart)"
  DBOPS_NAME="$(get_sgdbops_name restart)"

  create_or_replace_cluster "$CLUSTER_NAME" "$CLUSTER_NAMESPACE" 1

  wait_pods_running "$CLUSTER_NAMESPACE" 1
  wait_cluster "$CLUSTER_NAME" "$CLUSTER_NAMESPACE"

  generate_mock_data "$CLUSTER_NAME"
  check_mock_data_samehost "$CLUSTER_NAME"
}

e2e_test() {
   run_test "Checking that cluster restart does not fail if cluster failed" check_cluster_start_not_fail_if_cluster_failed

   run_test "Checking that cluster restart does not fail if pod failed" check_cluster_start_not_fail_if_pod_failed
}

check_cluster_start_not_fail_if_cluster_failed() {
  set_restarted_pods
  kubectl patch sgcluster -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME" --type json \
    -p '[{"op":"add","path":"/spec/pods/scheduling","value":{"nodeSelector":{"test":"test"}}}]'
  wait_until eval 'kubectl get sts -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME" -o json \
    | jq .spec.template.spec.nodeSelector.test | grep -q test'

  cat << EOF | kubectl create -f -
apiVersion: stackgres.io/v1
kind: SGDbOps
metadata:
  name: $DBOPS_FAILING_NAME
  namespace: $CLUSTER_NAMESPACE
spec:
  sgCluster: $CLUSTER_NAME
  op: restart
  timeout: PT15S
  restart:
    method: InPlace
EOF

  assert_dbops_failure "$DBOPS_FAILING_NAME" "$CLUSTER_NAMESPACE" 15

  wait_until kubectl patch sgcluster -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME" --type json \
    -p '[{"op":"remove","path":"/spec/pods/scheduling"}]'
  wait_until eval 'kubectl get sts -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME" -o json \
    | jq .spec.template.spec.nodeSelector | grep -q null'

  cat << EOF | kubectl create -f -
apiVersion: stackgres.io/v1
kind: SGDbOps
metadata:
  name: $DBOPS_NAME
  namespace: $CLUSTER_NAMESPACE
spec:
  sgCluster: $CLUSTER_NAME
  op: restart
  restart:
    method: InPlace
EOF

  check_restart
}

check_cluster_start_not_fail_if_pod_failed() {
  kubectl delete sgdbops -n "$CLUSTER_NAMESPACE" --all

  create_or_replace_cluster "$CLUSTER_NAME" "$CLUSTER_NAMESPACE" 2

  wait_pods_running "$CLUSTER_NAMESPACE" 2
  wait_cluster "$CLUSTER_NAME" "$CLUSTER_NAMESPACE"

  set_restarted_pods dbops-restart-with-failed-pod-1 dbops-restart-with-failed-pod-0
  kubectl patch sgcluster -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME" --type json \
    -p '[{"op":"add","path":"/spec/pods/scheduling","value":{"nodeSelector":{"test":"test"}}}]'
  wait_until eval 'kubectl get sts -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME" -o json \
    | jq .spec.template.spec.nodeSelector.test | grep -q test'

  cat << EOF | kubectl create -f -
apiVersion: stackgres.io/v1
kind: SGDbOps
metadata:
  name: $DBOPS_FAILING_NAME
  namespace: $CLUSTER_NAMESPACE
spec:
  sgCluster: $CLUSTER_NAME
  op: restart
  timeout: PT15S
  restart:
    method: InPlace
EOF

  assert_dbops_failure "$DBOPS_FAILING_NAME" "$CLUSTER_NAMESPACE" 15

  wait_until kubectl patch sgcluster -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME" --type json \
    -p '[{"op":"remove","path":"/spec/pods/scheduling"}]'
  wait_until eval 'kubectl get sts -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME" -o json \
    | jq .spec.template.spec.nodeSelector | grep -q null'

  cat << EOF | kubectl create -f -
apiVersion: stackgres.io/v1
kind: SGDbOps
metadata:
  name: $DBOPS_NAME
  namespace: $CLUSTER_NAMESPACE
spec:
  sgCluster: $CLUSTER_NAME
  op: restart
  restart:
    method: InPlace
EOF

  check_restart
}